<?php

/**
 * 短链接转换接口
 */
class Core_Plugin_Pay_ShortUrl extends Core_Plugin_Pay_Client {

    function __construct($m) {
        $this->wx = Core_Plugin_WeiXin::$wx_app[$m];
        // 设置接口链接
        $this->url = "https://api.mch.weixin.qq.com/tools/shorturl";
        // 设置curl超时时间
        $this->curl_timeout = 30;
    }

    /**
     * 生成接口参数xml
     */
    function createXml() {
        try {
            if ($this->parameters["long_url"] == null) {
                throw new Exception("短链接转换接口中，缺少必填参数long_url！" . "<br>");
            }
            $this->parameters["appid"] = $this->wx['appid']; // 公众账号ID
            $this->parameters["mch_id"] = $this->wx['mchid']; // 商户号
            $this->parameters["nonce_str"] = $this->createNoncestr(); // 随机字符串
            $this->parameters["sign"] = $this->getSign($this->parameters); // 签名
            return $this->arrayToXml($this->parameters);
        } catch (Exception $e) {
            die($e->getMessage());
        }
    }

    /**
     * 获取prepay_id
     */
    function getShortUrl() {
        $this->postXml();
        $prepay_id = $this->result["short_url"];
        return $prepay_id;
    }
}